package com.edu.controller;

import com.edu.dao.OrderMapper;
import com.edu.data.Order;
import com.edu.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@Controller
@RequestMapping("/order")
public class OrderController {
    SqlSession sqlSession= MyBatisUtil.createSession();
    OrderMapper orderMapper=sqlSession.getMapper(OrderMapper.class);

    @RequestMapping("/toOrder")
    private void toOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        req.setAttribute("orders",orderMapper.findAllorders(null));
        req.getRequestDispatcher("../static/manage/order.jsp").forward(req,resp);
    }

    @RequestMapping("/updateOrder")
    private void updateOrder(HttpServletRequest req,HttpServletResponse resp) throws SQLException, IOException {
        Order order = new Order();
        order=orderMapper.findOrders(Long.parseLong(req.getParameter("entityId")));
        order.setEoId(Integer.parseInt(req.getParameter("entityId")));
        order.setEoStatus(Integer.parseInt(req.getParameter("status")));
        orderMapper.updateOrder(order);
        resp.sendRedirect("toOrder");
    }

    @RequestMapping("/selectOrder")
    private void selectOrder(HttpServletRequest req,HttpServletResponse resp) throws SQLException, ServletException, IOException {
        req.setAttribute("orders",orderMapper.findAllorders(Long.parseLong(req.getParameter("entityId"))));
        req.setAttribute("entityId",req.getParameter("entityId"));
        req.getRequestDispatcher("../static/manage/order.jsp").forward(req,resp);
    }
}
